package com.java.system.controller;

import cn.hutool.core.util.StrUtil;
import com.java.common.utils.PageUtils;
import com.java.common.utils.R;
import com.java.common.utils.UuidUtil;
import com.java.system.entity.RoleUser;
import com.java.system.service.RoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/roleUser")
public class RoleUserController {

    @Autowired
    private RoleUserService roleUserService;

    /**
     * 带条件分页查询角色用户关联
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = roleUserService.queryPage(params);

        return R.ok(page);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public R save(@RequestBody RoleUser roleUser) {
        if (StrUtil.isNotBlank(roleUser.getId())) {
            roleUserService.updateById(roleUser);
        } else{
            roleUser.setId(UuidUtil.getShortUuid());
            roleUserService.save(roleUser);
        }
        return R.ok(roleUser);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        roleUserService.removeById(id);
        return R.ok();
    }

}
